// pages/teacher/chengji/chengji.js
Page({
  data: {
    semesterList: ['2023-2024学年第一学期', '2022-2023学年第二学期', '2022-2023学年第一学期'],
    semesterIndex: 0,
    courseList: [],
    courseStats: {
      totalCourses: 0,
      completedCourses: 0,
      pendingCourses: 0,
      averageScore: 0
    },
    isLoading: false
  },

  onLoad: function() {
    this.fetchCourseList();
  },
  
  onShow: function() {
    // 页面显示时刷新数据
    this.fetchCourseList();
  },
  
  // 学期选择
  semesterChange: function(e) {
    this.setData({
      semesterIndex: e.detail.value
    });
    
    this.fetchCourseList();
  },
  
  // 获取课程列表
  fetchCourseList: function() {
    this.setData({ isLoading: true });
    
    const semester = this.data.semesterList[this.data.semesterIndex];
    
    wx.showLoading({
      title: '加载中...',
    });
    
    wx.request({
      url: 'http://localhost:3000/api/grades/teacher/courses',
      method: 'GET',
      data: {
        semester: semester
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        wx.hideLoading();
        
        if (res.statusCode === 200) {
          this.setData({
            courseList: res.data.courses,
            courseStats: res.data.stats,
            isLoading: false
          });
        } else {
          this.setData({ isLoading: false });
          wx.showToast({
            title: res.data.message || '获取课程数据失败',
            icon: 'none'
          });
          
          // 使用模拟数据
          const mockData = this.getMockData();
          this.setData({
            courseList: mockData.courses,
            courseStats: mockData.stats
          });
        }
      },
      fail: () => {
        wx.hideLoading();
        this.setData({ isLoading: false });
        
        // 使用模拟数据
        const mockData = this.getMockData();
        this.setData({
          courseList: mockData.courses,
          courseStats: mockData.stats
        });
      }
    });
  },
  
  // 跳转到成绩录入页面
  goToGradeInput: function(e) {
    const courseId = e.currentTarget.dataset.id;
    const courseName = e.currentTarget.dataset.name;
    
    wx.navigateTo({
      url: `/pages/teacher/chengjiluru/chengjiluru?courseId=${courseId}&courseName=${courseName}`
    });
  },
  
  // 获取模拟数据
  getMockData: function() {
    return {
      courses: [
        {
          id: 1,
          courseName: '高等数学',
          className: '2023级计算机科学1班',
          studentCount: 45,
          gradedCount: 45,
          status: '已完成',
          averageScore: 82.5,
          passRate: 95.6
        },
        {
          id: 2,
          courseName: '线性代数',
          className: '2023级软件工程1班',
          studentCount: 50,
          gradedCount: 30,
          status: '进行中',
          averageScore: 78.3,
          passRate: 90.0
        },
        {
          id: 3,
          courseName: '数据结构',
          className: '2022级计算机科学1班',
          studentCount: 48,
          gradedCount: 0,
          status: '未开始',
          averageScore: 0,
          passRate: 0
        }
      ],
      stats: {
        totalCourses: 3,
        completedCourses: 1,
        pendingCourses: 2,
        averageScore: 80.4
      }
    };
  },
  
  // 下拉刷新
  onPullDownRefresh: function() {
    this.fetchCourseList();
    wx.stopPullDownRefresh();
  }
});